//
//  PHUserAccountView.swift
//  PHWeibo
//
//  Created by 潘荟 on 16/7/22.
//  Copyright © 2016年 BAT. All rights reserved.
//

import UIKit

class PHUserAccountView: NSObject {
    
    //定义本地路径
    let filePath = (NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("userAccount.archiver")
    
    
    
    var userAccount:PHUserAccount?

    
    //全局访问点
    static let sharedTools : PHUserAccountView = PHUserAccountView()
    
    var accessToken:String? {
        if userAccount?.access_token != nil {
            if userAccount?.expires_Date?.compare(NSDate()) == NSComparisonResult.OrderedDescending {
                return userAccount?.access_token
            }else {
                return nil
            }
        }else {
            return nil
        }
    }
    
    //判断用户是否登录,如果access_token为空,则表示未登录
    
    var isLogin:Bool {
        return accessToken != nil
    }
    
    override init() {
        super.init()
        
        userAccount = getUserAccount()
    }
}

//MARK: - 归档和解档
extension PHUserAccountView {
    func saveUaerAccount(userAccount:PHUserAccount) {
        
        // 为了解决第一次的userAccount的bug
        self.userAccount = userAccount
        
        NSKeyedArchiver.archiveRootObject(userAccount, toFile: filePath)
        
//        print(filePath)
    }
    
    func getUserAccount()-> PHUserAccount? {
        let result =  NSKeyedUnarchiver.unarchiveObjectWithFile(filePath) as? PHUserAccount
        
        return result
    }
}



//MARK: - 请求数据
extension PHUserAccountView {
    //请求accessToken数据
    func getUserAccount(code:String,isSuccess:(isSucc: Bool)->()) {
        PHNetworktools.sharedTools.oauthLoadUserAccount(code, success: { (response) -> () in
            
            guard let res = response as? [String : AnyObject] else {
                
                isSuccess(isSucc: false)
                return
            }
            
            let userAccount = PHUserAccount(dict: res)
            
            self.getUserInfo(userAccount, isSuccess: isSuccess)
            
            }) { (error) -> () in
                print("请求失败:\(error)")
                isSuccess(isSucc: false)
        }
    }
    
    
    
    //请求用户信息数据
    func getUserInfo(userAccount : PHUserAccount,isSuccess:(isSucc: Bool)->()) {
        PHNetworktools.sharedTools.oauthLoadUserInfo(userAccount, success: { (response) -> () in
            guard let res = response as? [String : AnyObject] else {
                isSuccess(isSucc: false)
                return
            }
            
            userAccount.screen_name = res["screen_name"] as? String
            userAccount.avatar_large = res["avatar_large"] as? String
            
            //保存数据
            self.saveUaerAccount(userAccount)
            
            isSuccess(isSucc: true)
            
            }) { (error) -> () in
                print("请求失败:\(error)")
                isSuccess(isSucc: false)
        }
    }
}